package com.ruoyi.job.task;

import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.job.domain.EtlTask;
import com.ruoyi.job.kettle.KettleQuartz;
import com.ruoyi.job.mapper.EtlTaskMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component("kettleTask")
public class KettleTask {
    private Logger logger = LoggerFactory.getLogger(KettleTask.class);

    //待校验表
    @Value("${tableNames}")
    private String[] tableNames;

    @Autowired
    private RedisService redisService;

    @Autowired
    private EtlTaskMapper etlTaskMapper;

    public void ryNoParams() {
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        String currentday=sdf.format(new Date(date.getTime()));
        logger.info("当前下载数据日期==》{}",currentday);
        logger.info("待下发表共计{}张",tableNames.length);


        for (int i = 0; i<tableNames.length; i++) {
            String tableName = tableNames[i];

            StringBuffer  authKey = new StringBuffer();
            authKey.append("authKey-").append(currentday).append("-").append(tableName);
            if("0".equals(redisService.getCacheObject(authKey.toString()))) {
                logger.info("当前表{}正在入库执行中",tableName);
                continue;
            } else if( "1".equals(redisService.getCacheObject(authKey.toString()))) {
                logger.info("当前表{}已下发入库完成",tableName);
                continue;
            } else {
                EtlTask task = new EtlTask();
                task.setTableName(tableName);
                task.setTaskDate(currentday);

                EtlTask etlTask = etlTaskMapper.findAllTaskStatus(task);
                logger.info("{}尚未下发，5分钟后重试",tableName);
                //当前任务已完成，然后可传输数据
                if(etlTask != null && "SUCCEEDED".equals(etlTask.getStatus())) {

                    redisService.setCacheObject(authKey.toString(), "0");  //执行中

                    logger.info("当前表 ===》" + etlTask.getTableName() + ",当前状态" + etlTask.getStatus());
                    if ("DATA_GTGSH_YZ".equals(etlTask.getTableName())) {
                        Map<String, String> map = new HashMap<String, String>();
                        map.put("qmcy_path","/home/ETLitem");
                        boolean bool = KettleQuartz.runKettleJob(map, "/home/ETLitem/DATA_GTGSH_YZ.kjb");
                        if(bool) {
                            redisService.setCacheObject(authKey.toString(), "1"); //执行完成
                        }
                    } else {
                        boolean bool = KettleQuartz.runKettleTransfer(null, "/home/ETLitem/"+ etlTask.getTableName()+".ktr");
                        if(bool) {
                            redisService.setCacheObject(authKey.toString(), "1"); //执行完成
                        }
                    }

                }
            }

        }

    }
}
